/**
 * 自动生成 mer_dictionary
 * 
 * Public/js/mp/mer_dictionary.js
 * Zix @ 2015-07-02
 */

var MerDictionary = {
  config: {
  },
  init: function (config) {
    var _this = this;
    $.extend(_this.config, config);
    Layout.set_menu_select(_this.config.menu_id);

    loading();
    _this.init_btn();
    _this.init_grid();
    sessionStorage.removeItem("scroll_top");
  },
    
  init_btn: function () {
    var _this = this;

    //查询按钮
    $(document).on('click', '#search_btn', function (e) {
      e.preventDefault();
      var query_param = $('#data_table').TableGrid('getparam');
      query_param.page = 1;

      $('#data_table').TableGrid('param', query_param);
      $('#data_table').TableGrid('reload');
    });
    
    //打开添加窗口
    $(document).on('click', '.load_add_modal_btn', function (e) {
      e.preventDefault();

      $('#submit_form').attr('action', _this.config.create_url);
      $('#submit_form')[0].reset();
      fillform( $('#submit_form') , _this.config.row );
      
      $('#action_label').html('新增');
      $('#add_edit_modal').modal('show');
    });

    //打开编辑窗口
    $(document).on('click', '.edit_btn', function (e) {
      e.preventDefault();
      
      var index = $('.edit_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);

      fillform( $('#submit_form') , data );
      $('#submit_form').attr('action', _this.config.update_url + '/' + data['id'] );
     
      $('#action_label').html('编辑');
      $('#add_edit_modal').modal('show');
    });

    //提交
    $(document).on('click', '#submit_btn', function (e) {
      e.preventDefault();

      var _thisbtn = $(this);
      var url = $('#submit_form').attr('action');
      var data = $('#submit_form').serializeObject();

      if (validform($('#submit_form'))) {
        data.mer_id = _this.config.mer_id ;
        data.pid = _this.config.pid ;
        $.post(url, data, function (ret) {
          tips(ret.info) ;
          if (ret.status) {
            $('#add_edit_modal').modal('hide');
            var query_param = $('#data_table').TableGrid('getparam');
            query_param.page = 1 ;
            $('#data_table').TableGrid('param' , query_param);
            $('#data_table').TableGrid('reload');
          }
        });
      }
    });
    
    //批量删除按钮
    $(document).on('click', '.del-btn', function (e) {
      e.preventDefault();
      var select_id = new Array();
      $("input[name='tablegrid-select[]']").each(function (index) {
        if ($(this).prop('checked')) {
          //取表数据
          var row = $('#data_table').TableGrid('getrow', index);
          select_id.push(row.id);
        }
      });

      if (select_id.length == 0) {
        tips('请选择数据');
      } else {
        _this.config.del_id = select_id.join(',');
        _this.del_data();
      }
    });

    //单个删除按钮
    $(document).on('click', '.del_one_btn', function (e) {
      e.preventDefault();
      var index = $('.del_one_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);
      _this.config.del_id = data['id'];
      _this.del_data();
    });
    
    //detail_btn
    $(document).on('click', '.detail_btn', function (e) {
      e.preventDefault();
      var url = _this.config.module_url + $(this).data( 'id' ) ;
      window.location.href = url ;
    });
    
    //查看下属数据
    $(document).on('click', '.son_btn', function (e) {
      e.preventDefault();
      var index = $('.son_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);
      var url = _this.config.module_url + '/' + data.id ;
      
      window.location.href = url ;
    });
  },
  
  init_grid: function () {
    var _this = this;
    $('#data_table').TableGrid({
      url: _this.config.read_url,
      select_all: false,
      load_after_init: true,
      param : { 
        mer_id : _this.config.mer_id ,
        pid : _this.config.pid ,
      } ,
      row_style: function (row, index) {
        if (row.status == 1 ) {
          return 'info';
        }
      }
    });
  },
  
  del_data: function (ids) {
    var _this = this;

    sure.init('确定删除吗？', function () {
      var url = _this.config.delete_url;
      var data = {ids: _this.config.del_id};

      $.post(url, data, function (ret) {
        tips(ret.info);
        if (ret.status) {
          $('#data_table').TableGrid('reload');
        }
      });
    });
  },
};

var format_is_del = function(value , row , index) {
  var v = Param.is_del[ value ]
  return value == 1 ? v : '<span class="deny">'+ v +'</span>' ; 
}

var opt_son = function(value , row , index) {
  return '<a href="#" data-index="'+ index +'" class="btn btn-info son_btn"><i class="fa fa-sitemap"></i> 查看下属</a> '; 
}

var opt_edit = function(value, row, index) {
  if ( row.mer_id == MerDictionary.config.mer_id ) {
    return '<a class="btn btn-warning edit_btn" data-id="' + row.id
        + '" href="#" taget="_blank" title="编辑"><i class="fa fa-pencil-square-o "></i> 编辑</a>';
  } else {
    return '';
  }
}

var opt_delete = function(value , row , index) {
  return row.allow_del == 1 ? '<a href="#" data-id="'+ row.id +'" class="btn btn-danger del_one_btn">删除</a> ' : '';  
}






